package com.caigou.controller;

import com.caigou.bean.cms.param.FooterConfigParam;
import com.caigou.bean.exam.entity.Users;
import com.caigou.enums.ResponseEnum;
import com.caigou.security.utils.CurrentUser;
import com.caigou.service.FooterConfigService;
import com.caigou.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@Slf4j
@RestController
@RequestMapping("/footer/")
public class FooterConfigController {

    @Resource
    private FooterConfigService footerConfigService;

    @RequestMapping("addFooterImage")
    public Result addFooterImage(@RequestBody FooterConfigParam param, @CurrentUser Users user){
        if(param.getFooterContent()!=null && param.getFooterContent().length()>128){
            return Result.error("页脚logo不能超过128个字", "页脚logo不能超过128个字");
        }
        if(param.getFooterUrl()!=null && param.getFooterUrl().length()>128){
            return Result.error("页脚logo链接不能超过128个字", "页脚logo链接不能超过128个字");
        }
        return footerConfigService.saveFooterImage(param);
    }

    @RequestMapping("addFooterText")
    public Result addFooterText(@RequestBody FooterConfigParam param, @CurrentUser Users user){
        if(param.getFooterContent()!=null && param.getFooterContent().length()>32){
            return Result.error("页脚文字不能超过128个字", "页脚文字不能超过128个字");
        }
        if(param.getFooterUrl()!=null && param.getFooterUrl().length()>128){
            return Result.error("页脚文字链接不能超过128个字", "页脚文字链接不能超过128个字");
        }
        return footerConfigService.saveFooterText(param);
    }

    @RequestMapping("updateFooterImage")
    public Result updateFooterImage(@RequestBody FooterConfigParam param, @CurrentUser Users user){
        if(param.getFooterContent()!=null && param.getFooterContent().length()>128){
            return Result.error("页脚logo不能超过128个字", "页脚logo不能超过128个字");
        }
        if(param.getFooterUrl()!=null && param.getFooterUrl().length()>128){
            return Result.error("页脚logo链接不能超过128个字", "页脚logo链接不能超过128个字");
        }
        return footerConfigService.updateFooterImage(param);
    }

    @RequestMapping("updateFooterText")
    public Result updateFooterText(@RequestBody FooterConfigParam param, @CurrentUser Users user){
        if(param.getFooterContent()!=null && param.getFooterContent().length()>32){
            return Result.error("页脚文字超过32个字", "页脚文字不能超过32个字");
        }
        if(param.getFooterUrl()!=null && param.getFooterUrl().length()>128){
            return Result.error("页脚文字链接不能超过128个字", "页脚文字链接不能超过128个字");
        }
        return footerConfigService.updateFooterText(param);
    }

    @RequestMapping("listFooterText")
    public Result listFooterText(@CurrentUser Users user){
        return footerConfigService.listFooterText();
    }

    @RequestMapping("deleteFooterText")
    public Result deleteFooterText(@RequestBody FooterConfigParam param, @CurrentUser Users user){
        return footerConfigService.deleteByPrimaryKey(param.getId());
    }

    @RequestMapping("getFooterImage")
    public Result getFooterImage(@CurrentUser Users user){
        return footerConfigService.getFooterImage();
    }

    @RequestMapping("publish")
    public Result publish(@CurrentUser Users user){
        return footerConfigService.publish();
    }

}
